package com.person.jc.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.plugin.activerecord.Db;

/**
 * ***************************************************************************
 * @程序名: CreateCode.java
 * @日期 : Aug 21, 2013
 * @作者 : 林坚端   (linjianduan@vvise.com)
 * @模块 : [项目]-[一级菜单]-[二级菜单]-[三级菜单] 
 * @描述 : [生成项目中所需要的所有编码] 
 * ------------------------------------------------------------ 
 * 修改历史 
 * 序号             日期                      修改人                  修改原因 
 * 1
 * 2
*****************************************************************************
 */
public class CreateCode {
	
	/**
	 * 
	 * @Description: [生成流水号- 13050113303099123456]
	 * @Author: 林坚端   (linjianduan@vvise.com)
	 * @Date: Aug 21, 2013
	 * @@return
	 */
	public static synchronized String generateKey() {
		try{
			Thread.sleep(10);
		}catch(Exception ex){
		}
		String key = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssms");
		key = sdf.format(new Date())
				+ String.valueOf((int) (Math.random() * 10000));
		for (; key.length() < 20;) {
			key = key + "0";
		}
		return key;
	}
	
	/**
	 * 
	 * @Description: [根据机构ID生成主键也可用于全表查询]
	 * @Author: 武智雄 (491933243@qq.com)
	 * @Date: 2014-3-7
	 * @@param companyId
	 * @@param tableName
	 * @@param idName
	 * @@param padLen
	 * @@return
	 */
    public static String createIdByCompanyId(String companyId,String tableName,String idCol,int padLen,String companyCol,String andSql){        
        String sql = "SELECT lpad(nvl(MAX(to_number(SUBSTR("+idCol+","+(companyId.length()+1)+","+padLen+")))+1,1),"+ padLen +",'0') max_id FROM "+tableName+" WHERE ";
        sql += " 1 = 1";
        if(Utils.isNotEmpty(companyCol))
            sql += " and "+ companyCol +" = '"+companyId+"' ";
        
        if(Utils.isNotEmpty(andSql))
            sql += " " + andSql;
        sql += " ORDER BY "+idCol+" DESC";
        
        String code = Db.queryStr(sql);
        
        return companyId + code;
    }
    
    /**
     * 
     * @Description: [根据机构ID生成主键-填充不存在主键]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: 2014-5-20
     * @@param companyId
     * @@param tableName
     * @@param idCol
     * @@param padLen
     * @@param companyCol
     * @@param andSql
     * @@return
     */
    public static String createIdByCompanyId2(String companyId,String tableName,String idCol,int padLen,String companyCol,String andSql){        
        String sql = "SELECT lpad(nvl(min(val),1),"+ padLen +",'0') max_id ";
        sql += " from ( select rownum val from "+ tableName ;
        sql += " minus";
        sql += " select to_number(substr("+idCol+","+(companyId.length()+1)+")) val";
        sql += "  from "+ tableName +" where 1 = 1";
        if(Utils.isNotEmpty(companyCol))
            sql += " and "+ companyCol +" = '"+companyId+"' ";
        
        if(Utils.isNotEmpty(andSql))
            sql += " " + andSql;
        
        sql += ")";
        
        String code = Db.queryStr(sql);
        
        return companyId + code;
    }
    public static String createDeptId(String pDeptId) {
		String sql = " select lpad(max(ifnull(substr(t.dept_id, "+(pDeptId.length()+1)+", 2),0))+1,2,'0')from ts_dept t where t.dept_id like '" + pDeptId + "%'";
		String code = Db.queryStr(sql);
		return pDeptId + code;
	}
   /**
    * 
    * @Description: [初始化最大值加1]
    * @Author: wuyang (wuyang@vvise.com)
    * @Date: 2015-12-28
    * @param initV
    * @param table
    * @return
    */
    public static String createCodeByRule(String initV,String col,String table){
    	String sql = 
    		"SELECT CASE\n" +
    		"         WHEN MAX(T."+col+") IS NULL THEN\n" + 
    		"          "+initV+"\n" + 
    		"         ELSE\n" + 
    		"          NVL(MAX(T."+col+"), "+initV+") + 1\n" + 
    		"       END\n" + 
    		"  FROM "+table+" T";
    	return Db.queryStr(sql);

    }
    
    public static String createOrderId(String orderType){
    	String sql = "SELECT CONCAT('XYZC-',CASE WHEN ? = 1 THEN 'JC' ELSE 'WX' END,'-',DATE_FORMAT(NOW(),'%Y%m%d'),'-',LPAD(IFNULL(MAX(SUBSTR(order_id, 18, 2)), '0')+1,2,'0'))  FROM bs_order WHERE order_type = ?" +
    			"	 AND order_id LIKE CONCAT('XYZC-',CASE WHEN ? = 1 THEN 'JC' ELSE 'WX' END,'-',DATE_FORMAT(NOW(),'%Y%m%d'), '%')";
    	return Db.queryStr(sql, orderType, orderType, orderType);
    }
    
    public static String createOrderNo(String orderType){
    	String sql = "select concat(date_format(now(),'%Y%m%d'),LPAD(IFNULL(MAX(SUBSTR(order_no, 9, 2)), '0')+1,2,'0'))  from bs_order where order_type = ? and order_no like concat(date_format(now(),'%Y%m%d'),'%') ";
    	return Db.queryStr(sql, orderType);
    }
    
    public static String createBuyId(){
    	String sql = "SELECT CONCAT('XYZC-','CG','-',DATE_FORMAT(NOW(),'%Y%m%d'),'-',LPAD(IFNULL(MAX(SUBSTR(log_code, 18, 2)), '0')+1,2,'0'))  FROM bs_buy_apply  ";
    	return Db.queryStr(sql);
    }
    
    public static String createBuyNo(){
    	String sql = "SELECT CONCAT(DATE_FORMAT(NOW(),'%Y'),LPAD(IFNULL(MAX(SUBSTR(buy_code, 5, 4)), '0')+1,4,'0'))  FROM bs_buy_apply  ";
    	return Db.queryStr(sql);
    }
}
